// 生成路由
import { markRaw } from "vue";
import Layout from '@/layout/index.vue'
const modules = import.meta.glob('./../views/**/*.vue')
// console.log(modules)
// 递归过滤异步路由表 转换为组件对象
export function filterAsyncRoutes(routes) {
  const accessRoutes = routes.filter(route => {
    if (route.component) {
      if (route.component === 'Layout') { // Layout组件特殊处理
        route.component = markRaw(Layout)
      } else {
        const component = route.component
        route.component = loadView(route.component)
      }
    }
    if (route.children && route.children.length) {
      route.children = filterAsyncRoutes(route.children)
    }
    return true
  })
  return accessRoutes
}

function loadView(view) {
  let res;
  for (let path in modules) {
    // ./../views/system/users.vue
    // 这步相当于取中间的一节/system/users
    const dir = path.split('views')[1].split('.vue')[0];
    if (dir === view) {
      res = () => modules[path]();
    }
  }
  return res
}